window.onload = function(){
    getProduct();
}
var baseUrl = "http://localhost:8099";

var categoryName = $("#categoryName").text();

function getProduct(){

    console.log(categoryName);
    $.ajax({
        url: baseUrl + "/product/getProductList",  //后台接口
        type: "post",
        data: JSON.stringify({"page":"1","limit":"4","categoryName":categoryName}),
        contentType: "application/json; charset=utf-8",
        datatype:"json",
        success: function (data) {
            let json = JSON.parse(JSON.stringify(data))
            console.log(json)
            layui.use(['laypage','layer'], function(){
                var laypage = layui.laypage
                    ,layer = layui.layer
                    ,$ = layui.$;
                //完整功能
                laypage.render({
                    elem: 'page'
                    ,count: json.data.totalCount
                    ,theme: '#1E9FFF'
                    ,limit: 4
                    ,limits: [4,8,12,16,20,24]
                    ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                    ,jump: function(obj,first){
                        $.ajax({
                            url: baseUrl + "/product/getProductList",
                            type: "post",
                            data: JSON.stringify({"page":""+obj.curr,"limit":""+obj.limit,"categoryName":categoryName}),
                            contentType: "application/json; charset=utf-8",
                            datatype: "json",
                            success: function (data) {
                                let json = JSON.parse(JSON.stringify(data));
                                console.log(json);
                                this.count = json.data.totalCount;
                                console.log("count:",this.count);
                                let li = '';
                                for(let i = 0; i < json.data.list.length; i++){
                                    li = li + "<li class=\"box_one\">\n" +
                                        "<a href=\"http://localhost:8099/goods/detail?id="+ json.data.list[i].id +"\">\n" +
                                        "<ul class=\"main_ul\">\n" +
                                        "<li class=\"main_li\"><img src=\""+ json.data.list[i].picture +"\"width=\"306px\" height=\"306px\"></li>\n" +
                                        "<li class=\"flower_language\">"+json.data.list[i].productName+"</li>\n" +
                                        "<li class=\"price\">￥"+json.data.list[i].productPrice+"</li>\n" +
                                        "<li class=\"download_app\">下载Flower鲜花APP,此款立省50元</li>\n" +
                                        "</ul>\n" +
                                        "</a>\n" +
                                        "</li>"
                                }
                                $("#cake_product").children("li").remove(); // 移除展示的商品
                                $("#cake_product").html(li); // 添加商品
                            }

                        })
                    }
                });

            });
        }

    })
}
